#ifndef ARRAY_H
#define ARRAY_H

#include <assert.h>
#include <stdlib.h>
#include "common.h"



/**
	'Templata' wykorzystujaca makro preprocesor.
	
	Na przyklad, Array(int) wraz z DefArray(int) powoduje stworzenie struktury
	intArray wraz z funkcjami
	- intArray_new
	- intArray_delete
	- intArray_append
*/

#define Array(T)  \
T##Array T##Array_new() {  \
   T##Array res;  \
   res.length = res.capacity = 0;   \
   res.data = null;  \
   return res; \
}  \
void T##Array_delete(T##Array* arr) {  \
   assert (arr != null);   \
   free(arr->data);  \
   arr->length = arr->capacity = 0; \
   arr->data = null; \
}  \
void T##Array_append(T##Array* arr, T x) {   \
   assert (arr != null); \
   if (arr->length >= arr->capacity) { \
      arr->capacity = arr->capacity == 0 ? 4 : arr->capacity*2;   \
      arr->data = cast(T*)realloc(arr->data, arr->capacity * sizeof(T));   \
   }  \
   arr->data[arr->length++] = x; \
}


#define DefArray(T)  \
typedef struct {  \
   int   length;  \
   int   capacity;   \
   T*    data; \
} T##Array;    \
T##Array T##Array_new();  \
void T##Array_delete(T##Array* arr);  \
void T##Array_append(T##Array* arr, T x);




#endif	// ARRAY_H
